/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChangePasswordDlg.java
 *
 * Created on Jul 10, 2011, 10:51:37 AM
 */
package com.viweb.storemanager.ui;

import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.DESPasswordEncrypt;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.Employee;
import javax.swing.JOptionPane;

/**
 *
 * @author qhvic
 */
public class ChangePasswordDlg extends javax.swing.JDialog {

    private Employee emp = null;

    /** Creates new form ChangePasswordDlg */
    public ChangePasswordDlg(Employee emp, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.emp = emp;
        extraInitComponents();
    }

    private void extraInitComponents() {
        this.txtUsername.setText(this.emp.getUsername());
        this.txtOldPassword.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        lblOldPass = new javax.swing.JLabel();
        lblNewPass = new javax.swing.JLabel();
        lblRelyPass = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtOldPassword = new javax.swing.JPasswordField();
        txtNewPassword = new javax.swing.JPasswordField();
        txtRelyPasword = new javax.swing.JPasswordField();
        btnOk = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Đổi mật khẩu");
        setMinimumSize(new java.awt.Dimension(251, 177));
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin tài khoản", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.blue)); // NOI18N
        jPanel1.setMinimumSize(new java.awt.Dimension(247, 132));
        jPanel1.setPreferredSize(new java.awt.Dimension(247, 132));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        lblUsername.setText("Tên đăng nhập:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(lblUsername, gridBagConstraints);

        lblOldPass.setText("Mật khẩu:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 0, 0);
        jPanel1.add(lblOldPass, gridBagConstraints);

        lblNewPass.setText("Mật khẩu mới:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 0, 0);
        jPanel1.add(lblNewPass, gridBagConstraints);

        lblRelyPass.setText("Xác nhận:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 2, 2);
        jPanel1.add(lblRelyPass, gridBagConstraints);

        txtUsername.setEnabled(false);
        txtUsername.setMinimumSize(new java.awt.Dimension(150, 22));
        txtUsername.setPreferredSize(new java.awt.Dimension(150, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 2);
        jPanel1.add(txtUsername, gridBagConstraints);

        txtOldPassword.setMinimumSize(new java.awt.Dimension(150, 22));
        txtOldPassword.setPreferredSize(new java.awt.Dimension(150, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 2);
        jPanel1.add(txtOldPassword, gridBagConstraints);

        txtNewPassword.setMinimumSize(new java.awt.Dimension(150, 22));
        txtNewPassword.setPreferredSize(new java.awt.Dimension(150, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 2);
        jPanel1.add(txtNewPassword, gridBagConstraints);

        txtRelyPasword.setMinimumSize(new java.awt.Dimension(150, 22));
        txtRelyPasword.setPreferredSize(new java.awt.Dimension(150, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 2, 2);
        jPanel1.add(txtRelyPasword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        getContentPane().add(jPanel1, gridBagConstraints);

        btnOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/ok.png"))); // NOI18N
        btnOk.setText("Đồng ý");
        btnOk.setToolTipText("Lưu thay đổi mật khẩu");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(15, 5, 5, 0);
        getContentPane().add(btnOk, gridBagConstraints);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/media_stop_red.png"))); // NOI18N
        btnCancel.setText("Hủy bỏ");
        btnCancel.setToolTipText("Đóng cửa sổ thay đổi mật khẩu");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(15, 5, 5, 5);
        getContentPane().add(btnCancel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        // TODO add your handling code here:
        try {
            Biz biz = new Biz();
            CursorManager.setWaitCursor(this);
            String password = new String(this.txtOldPassword.getPassword());

            // Nhập mật khẩu không chính xác
            String realPassword = DESPasswordEncrypt.getInstance().decrypt(this.emp.getPassword());
            if (!password.equals(realPassword)) {
                OptionPane.show(JOptionPane.getFrameForComponent(ChangePasswordDlg.this),
                        "Đổi mật khẩu không thành công. Mật khẩu hiện tại không chính xác!", "Thông báo",
                        OptionPane.MsgTYPE_WARNING);
                this.txtOldPassword.setText("");
                this.txtNewPassword.setText("");
                this.txtRelyPasword.setText("");
                this.txtOldPassword.requestFocus();
                CursorManager.setDefaultCursor(this);
            } else {
                // Nhập mật khẩu chính xác
                this.emp.setPassword(new String(this.txtNewPassword.getPassword()));
                String confirm = (new String(this.txtRelyPasword.getPassword()));

                boolean change = biz.changePassword(this.emp, confirm);
                // Đổi mật khẩu không thành công
                if (!change) {
                    OptionPane.show(JOptionPane.getFrameForComponent(ChangePasswordDlg.this),
                            "Đổi mật khẩu không thành công! " + biz.getLastError(), "Thông báo",
                            OptionPane.MsgTYPE_WARNING);
                    this.emp.setPassword(DESPasswordEncrypt.getInstance().decrypt(password));
                    this.txtNewPassword.setText("");
                    this.txtRelyPasword.setText("");
                    this.txtNewPassword.requestFocus();
                    CursorManager.setDefaultCursor(this);
                    // Nếu thành công
                } else {
                    CursorManager.setDefaultCursor(this);
                    this.dispose();
                }
            }
        } catch (Exception ex) {
        }
    }//GEN-LAST:event_btnOkActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOk;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblNewPass;
    private javax.swing.JLabel lblOldPass;
    private javax.swing.JLabel lblRelyPass;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPasswordField txtNewPassword;
    private javax.swing.JPasswordField txtOldPassword;
    private javax.swing.JPasswordField txtRelyPasword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
